/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.report.ui;

import java.util.List;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import org.springframework.context.ApplicationEvent;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.table.support.AbstractObjectTable;
import reportcenter.datasource.ui.DataSourceTable;
import reportcenter.report.business.AdminReportDao;
import reportcenter.report.dominio.report.Report;

/**
 *
 * @author renan_luz
 */
public class AdminReportTable extends AbstractObjectTable {

    /** Mantém todos os dados do relatórios */
    private AdminReportDao dao;

    public void refreshTable(){
        List list = getBaseEventList();
        list.clear();
        list.addAll(dao.getByRole());
    }
    
    
    /**
     * Default constructor.
     */
    public AdminReportTable(AdminReportDao dao) {
//        super("adminReports", new String[]{"title", "subtitle", "idD.uri", "query"});
        super("adminReports", new String[]{"title", "subtitle"});
        this.dao = dao;
    }

    @Override
    protected Object[] getDefaultInitialData() {
        return dao.getByRole().toArray();
//        return dao.getAll();
    }

    protected void configureTable(JTable table) {
        // Adjust the table column widths
        TableColumnModel tcm = table.getColumnModel();
        tcm.getColumn(0).setPreferredWidth(50);
        tcm.getColumn(1).setPreferredWidth(200);
//        tcm.getColumn(2).setPreferredWidth(50);
//        tcm.getColumn(3).setPreferredWidth(300);
    }

    public Report[] getSelectedReports() {
        int[] selected = getTable().getSelectedRows();
        Report[] reports = new Report[selected.length];
        for (int i = 0; i < selected.length; i++) {
            reports[i] = (Report) getTableModel().getElementAt(selected[i]);
        }
        return reports;
    }

    public Report getSelectedReport() {
        return (Report) getSelectedReports()[0];
    }

    @Override
    protected boolean shouldHandleEvent(ApplicationEvent event) {
        if (event instanceof LifecycleApplicationEvent) {
            Class<? extends Object> eventClass = (((LifecycleApplicationEvent) event).getObject().getClass());
            return getTableObjectsClass().isAssignableFrom(eventClass) && super.shouldHandleEvent(event);
        } else {
            return super.shouldHandleEvent(event);
        }
    }

    public Class<? extends Object> getTableObjectsClass() {
        return Report.class;
    }
}
